import 'package:fish_redux/fish_redux.dart';
import '../../../PublicSource/utils/compress_imagefile.dart';
import 'action.dart';
import 'state.dart';
import 'package:orientation/orientation.dart';
import 'package:flutter/services.dart';
import '../../../app_files.dart';

Effect<OrderSignViewState> buildEffect() {
  return combineEffects(<Object, Effect<OrderSignViewState>>{
    OrderSignViewAction.action: _onAction,
    OrderSignViewAction.onSave: _onSave,
    OrderSignViewAction.onClear: _onClear,
    Lifecycle.initState: _initState,
    Lifecycle.deactivate: _dispose,
  });
}

void _onAction(Action action, Context<OrderSignViewState> ctx) {}

void _initState(Action action, Context<OrderSignViewState> ctx) {
  OrientationPlugin.forceOrientation(DeviceOrientation.landscapeLeft);
}

void _dispose(Action action, Context<OrderSignViewState> ctx) {
  OrientationPlugin.forceOrientation(DeviceOrientation.portraitUp);
}

void _onSave(Action action, Context<OrderSignViewState> ctx) async {
  if (ctx.state.controller.isEmpty) {
    CommonUtils.showErrorInfo(ctx.context, '请手写签字后保存');
    return;
  }
  var data = await ctx.state.controller.toPngBytes();
  var file = await CompressImageFile.saveFile(data, path: '/sign.png');
  print(file);
  Navigator.pop(ctx.context, file);
}

void _onClear(Action action, Context<OrderSignViewState> ctx) {
  ctx.state.controller.clear();
}
